package com.test.spring;

import com.example.entity.Entity;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class LifeCycleBean implements InitializingBean,BeanNameAware,
        ApplicationContextAware,BeanFactoryAware {//这里没有加postprocessor结尾的，因为这些是对所有bean作用的，单独写
    private String name;

    private String                     beanName;

    private String definition;

    private ApplicationContext        applicationContext;

    private BeanFactory                beanFactory;

    public String getDefinition() {
        return definition;
    }

    public void setDefinition(String definition) {
        this.definition = definition;
    }

    public LifeCycleBean() {
        System.out.println("LifeCycleBean() over 1");
    }

    public LifeCycleBean(String name) {

        this.name = name;
        System.out.println("LifeCycleBean(TestString name) over ");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("set fangfa over 2");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("afterPropertiesSet() over 6");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("Enter AwareBean.setBeanFactory(), beanfactory = " + beanFactory + " 4 \n");
        this.beanFactory = beanFactory;
    }

    @Override
    public void setBeanName(String beanName) {
        System.out.println("Enter .setBeanName(), beanName = " + beanName + " 3 \n");
        this.beanName = beanName;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("Enter.setApplicationContext(), applicationContext = " + applicationContext + " 5 \n");
        this.applicationContext = applicationContext;
    }

    public String getBeanName() {
        return beanName;
    }

//    @Override
//    public Entity getObject() throws Exception {
//        System.out.println("get object 7");
//        Entity entity = new Entity();
//        entity.setName("object");
//        return entity;
//    }
//
//    @Override
//    public Class<?> getObjectType() {
//        return Entity.class;
//    }
//
//    @Override
//    public boolean isSingleton() {
//        return true;
//    }


    @Override
    public String toString() {
        return "LifeCycleBean{" +
                "name='" + name + '\'' +
                ", beanName='" + beanName + '\'' +
                ", definition='" + definition + '\'' +
                '}';
    }
}
